// ------------------------------------------------------------------------------------------
// Licensed by Interprise Solutions.
// http://www.InterpriseSolutions.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using System.Configuration;
using System.Reflection;

namespace InterpriseSuiteEcommerceCommon.InterpriseIntegration.Authentication
{
    internal sealed class RestrictedResourceConfiguration
    {
        private static string CONFIG_SECTION_NAME = "restrictedResource";
        private bool _isEnabled;
        private List<RestrictedResourceInfo> _protectedResources = new List<RestrictedResourceInfo>();

        private static RestrictedResourceConfiguration _config;

        private RestrictedResourceConfiguration() { }

        public static RestrictedResourceConfiguration Instance
        {
            get 
            {
                if (null == _config)
                {
                    _config = ConfigurationManager.GetSection(CONFIG_SECTION_NAME) as RestrictedResourceConfiguration;
                }

                return _config;
            }
        }

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set{ _isEnabled = value; }
        }

        public List<RestrictedResourceInfo> ProtectedResources
        {
            get { return _protectedResources; }
        }

        public static RestrictedResourceConfiguration CreateInstance(XmlNode section)
        {
            RestrictedResourceConfiguration config = new RestrictedResourceConfiguration();           

            XmlAttribute enabledAttribute = section.Attributes["enabled"];
            bool isEnabled = (null != enabledAttribute) && bool.TryParse(enabledAttribute.Value, out isEnabled) && isEnabled;
            config.IsEnabled = isEnabled;

            XmlNodeList protectedResourceNodes = section.SelectNodes("add");
            foreach (XmlNode protectedResourceNode in protectedResourceNodes)
            {
                RestrictedResourceInfo info = new RestrictedResourceInfo();
                info.Name = protectedResourceNode.Attributes["name"].Value;
                info.LoginUrl = protectedResourceNode.Attributes["loginUrl"].Value;
                info.DefaultUrl = protectedResourceNode.Attributes["defaultUrl"].Value;

                XmlAttribute timeOutAttribute = protectedResourceNode.Attributes["timeOut"];
                int timeOut = RestrictedResourceInfo.DEFAULT_TIMEOUT;
                if (null != timeOutAttribute && int.TryParse(timeOutAttribute.Value, out timeOut))
                {
                    info.TimeOut = timeOut;
                }

                string typeFullName = protectedResourceNode.Attributes["associatedPrincipal"].Value;
                info.AssociatedPrincipalType = Type.GetType(typeFullName);

                XmlNodeList patternNodes = protectedResourceNode.SelectNodes("qualifiers/add");
                foreach(XmlNode patternNode in patternNodes)
                {
                    info.Qualifier.Patterns.Add(patternNode.Attributes["value"].Value);
                }

                config.ProtectedResources.Add(info);
            }

            return config;
        }

    }
}
